<?php

	class EventItemMaterialBusiness {
		
		public function saveMaterial($material, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemMaterialDataAccess = DataAccessFactory::factory('EventItemMaterialDataAccessMySQL');
			
			$result = $eventItemMaterialDataAccess->saveMaterial($material, $dataBaseConnection);
			
			//faz o upload do arquivo
			if ($result != -1) {
				$material->setEventItemMaterialId($result);
				$material->setMaterialFileName(getMaterialName($material));
				$materialFileFullPath = getMaterialFullPath($material);

				if(move_uploaded_file($material->getMaterialTempName(), $materialFileFullPath)) {
				 	$result2 = $this->updateMaterialFileName($material, $dataBaseConnection);
				}
				else {
					$result = -1;
				}
			}
			
			if ($connectionOpened) {
				if ($result == -1) {
					$dataBaseConnection->rollback();
				} else {
					if (!$result2) {
						$dataBaseConnection->rollback();
						unlink($materialFileFullPath);
					}
					else {
						$dataBaseConnection->commit();
					}
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		
		public function updateMaterialFileName($material, $dataBaseConnection = null) {
			
			$connectionOpened = false;
			
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemMaterialDataAccess = DataAccessFactory::factory('EventItemMaterialDataAccessMySQL');
			
			$result = $eventItemMaterialDataAccess->updateMaterialFileName($material, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}	
		
		public function selectEventItemMaterial($eventItemMaterial, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemMaterialDataAccess = DataAccessFactory::factory('EventItemMaterialDataAccessMySQL');
			
			$result = $eventItemMaterialDataAccess->selectEventItemMaterial($eventItemMaterial, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!is_array($result)) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		public function selectEventItemMaterialByEventItemId($eventItemId) {
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$eventItemMaterialDataAccess = DataAccessFactory::factory('EventItemMaterialDataAccessMySQL');
			
			$result = $eventItemMaterialDataAccess->selectEventItemMaterialByEventItemId($eventItemId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
		
		public function deleteEventItemMaterialByEventItemAttendant($eventItem, $dataBaseConnection = null) {
			
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			$eventItemMaterial = new EventItemMaterial();
			$eventItemMaterial->setEventItemId($eventItem->eventItemId);
			$eventItemMaterial->setMaterialUser($eventItem->eventItemUser);
			
			$materialList = $this->selectEventItemMaterial($eventItemMaterial, $dataBaseConnection);
			
			$result = $this->deleteEventItemMaterial($eventItemMaterial, $dataBaseConnection);
			
			if ($result) {
				foreach ($materialList as $material) {
					unlink(getMaterialFullPath($material));
				}
			}
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
		
		public function deleteEventItemMaterial($eventItemMaterial, $dataBaseConnection) {
			
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemMaterialDataAccess = DataAccessFactory::factory('EventItemMaterialDataAccessMySQL');
			$result = $eventItemMaterialDataAccess->deleteEventItemMaterial($eventItemMaterial, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
	}

?>